<?php
class NotificationSendEmail extends Notification implements INotification
{
  private $from          = null;
  private $pathSendEmail = "";
  private $smtpHost      = null;
  private $smtpPassword  = null;
  private $smtpPort      = 25;
  private $smtpUsername  = null;
  private $subjectPrefix = "[lapbs]";
  private $tls           = "yes";
  private $to            = null;
  
  public function send($subject, $message)
  {
    $f = "";
    if($this->from!=null)
    {
      $f = " -f " . $this->from;
    }
    $t = "";
    if($this->to!=null)
    {
      $t = " -t " . $this->to;
    }
    $s = "";
    if($this->smtpHost!=null)
    {
      $s = " -s " . $this->smtpHost;
      if($this->smtpPort!=null)
      {
        $s.= ":" . $this->smtpPort;
      }
    }
    $tls = "";
    if($this->tls!=null)
    {
      $tls = " -o tls=" . $this->tls;
    }
    $xu = "";
    if($this->smtpUsername!=null)
    {
      $xu = " -xu " . $this->smtpUsername;
    }
    $xp = "";
    if($this->smtpPassword!=null)
    {
      $xp = " -xp " . $this->smtpPassword;
    }
    
    $u = " -u \"" . $this->subjectPrefix . " " . $subject . "\"";
    $m = " -m \"" . $message . "\"";

    $notifyCmd = $this->pathSendEmail . "sendEmail.pl " .$f . $t . $u . $m . $s . $tls . $xu . $xp;
    $notifyOutput = array();
    $notifyReturnValue = -1;
    $this->logger->info("Execute: '" . str_replace($this->smtpPassword, "*******", $notifyCmd) . "'");
    exec($notifyCmd, $notifyOutput, $notifyReturnValue);
    if($notifyReturnValue!=0)
    {
    	$this->logger->error("Error on sending notification!");
    }
  }
  
  private function setFrom($from)
  {
    $from = trim($from);
    $this->from = $from;
  }
  
  public function setOptions($emailElement)
  {
    foreach($emailElement->children() as $emailOptionElement)
    {
      switch($emailOptionElement->getName())
      {
        case "From":
          $this->setFrom((string)$emailOptionElement);
          break;
          
        case "PathSendEmail":
          $this->setPathSendEmail((string)$emailOptionElement);
          break;
        
        case "SubjectPrefix":
          $this->setSubjectPrefix((string)$emailOptionElement);
          break;
          
        case "SmtpHost":
          $this->setSmtpHost((string)$emailOptionElement);
          break;
          
        case "SmtpPort":
          $this->setSmtpPort((string)$emailOptionElement);
          break;
          
        case "SmtpUsername":
          $this->setSmtpUsername((string)$emailOptionElement);
          break;

        case "SmtpPassword":
          $this->setSmtpPassword((string)$emailOptionElement);
          break;
          
        case "Tls":
          $this->setTls((string)$emailOptionElement);
          break;          
          
        case "To":
          $this->setTo((string)$emailOptionElement);
          break;
                    
        default:
          $this->logger->info("Unknown email option element '" . $emailOptionElement->getName() ."'.");
          break;
      }
    }
  }
  
  private function setPathSendEmail($pathSendEmail)
  {
    $pathSendEmail = trim($pathSendEmail);
    
    if(strlen($pathSendEmail)>0 && substr($pathSendEmail, -1, 1)!=DIRECTORY_SEPARATOR)
    {
      $pathSendEmail = $pathSendEmail . DIRECTORY_SEPARATOR;
    }
    
    $this->pathSendEmail = $pathSendEmail;
  }   
  
  private function setSubjectPrefix($subjectPrefix)
  {
    $subjectPrefix = trim($subjectPrefix);
    $this->subjectPrefix = $subjectPrefix;
  }
  
  private function setSmtpHost($smtpHost)
  {
    $smtpHost = trim($smtpHost);
    $this->smtpHost = $smtpHost;
  }
  
  private function setSmtpPort($smtpPort)
  {
    $smtpPort = trim($smtpPort);
    $this->smtpPort = $smtpPort;
    
  }
  
  private function setSmtpUsername($smtpUsername)
  {
    $smtpUsername = trim($smtpUsername);
    $this->smtpUsername = $smtpUsername;    
  }
  
  private function setSmtpPassword($smtpPassword)
  {
    $smtpPassword = trim($smtpPassword);
    $this->smtpPassword = $smtpPassword;
  }
  
  private function setTls($tls)
  {
    $tls = trim($tls);
    $this->tls = $tls;
  }
  
  private function setTo($to)
  {
    $to = trim($to);
    $this->to = $to;
  }
}